package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>().eq("category1_id",category1Id));
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>().eq("category2_id",category2Id));
    }

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.attrInfoList(category1Id,category2Id,category3Id);
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        if(baseAttrInfo.getId()!=null){
            baseAttrInfoMapper.updateById(baseAttrInfo);
            for (BaseAttrValue baseAttrValue : baseAttrInfo.getAttrValueList()) {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                if(baseAttrValue.getId()!=null){
                    baseAttrValueMapper.updateById(baseAttrValue);
                }else{
                    baseAttrValueMapper.insert(baseAttrValue);
                }
            }
        }else{baseAttrInfoMapper.insert(baseAttrInfo);
            for (BaseAttrValue baseAttrValue : baseAttrInfo.getAttrValueList()) {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
    }

    @Override
    public IPage<BaseTrademark> baseTrademark(Integer page, Integer limit) {
        return baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page, limit), null);
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId));
        return baseAttrValueList;
    }

    @Override
    public BaseTrademark getBaseTrademark(Long id) {
        return baseTrademarkMapper.selectById(id);
    }

    @Override
    public void baseTrademarkUpdate(BaseTrademark trademark) {
        baseTrademarkMapper.updateById(trademark);
    }

    @Override
    public List<BaseTrademark> getTrademarkList() {
        List<BaseTrademark> list = baseTrademarkMapper.selectList(null);
        return list;
    }

    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }

    @Override
    public IPage<SpuInfo> spuList(Integer page, Integer limit, Long category3Id) {
        IPage<SpuInfo> spuInfoIPage = spuInfoMapper.selectPage(new Page<SpuInfo>(page, limit), new QueryWrapper<SpuInfo>().eq("category3_id", category3Id));
        return spuInfoIPage;
    }

    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        spuInfoMapper.insert(spuInfo);
        for (SpuImage spuImage : spuInfo.getSpuImageList()) {
            spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
        }
        for (SpuSaleAttr spuSaleAttr : spuInfo.getSpuSaleAttrList()) {
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrMapper.insert(spuSaleAttr);
            for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttr.getSpuSaleAttrValueList()) {
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                spuSaleAttrValueMapper.insert(spuSaleAttrValue);
            }
        }
    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {
        return spuImageMapper.selectList(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
    }

    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        skuInfoMapper.insert(skuInfo);
        for (SkuImage skuImage : skuInfo.getSkuImageList()) {
            skuImage.setSkuId(skuInfo.getId());
            skuImageMapper.insert(skuImage);
        }
        for (SkuAttrValue skuAttrValue : skuInfo.getSkuAttrValueList()) {
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);
        }
        for (SkuSaleAttrValue skuSaleAttrValue : skuInfo.getSkuSaleAttrValueList()) {
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        }
    }

    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
        //单表查询
        List<SpuSaleAttrValue> list = spuSaleAttrValueMapper.selectList(new QueryWrapper<SpuSaleAttrValue>().eq("spu_id",spuId));
        List<SpuSaleAttr> spuSaleAttrList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            boolean flag = true;
            for (SpuSaleAttr saleAttr : spuSaleAttrList) {
                if(list.get(i).getBaseSaleAttrId()==saleAttr.getBaseSaleAttrId()){
                    saleAttr.getSpuSaleAttrValueList().add(list.get(i));
                    flag = false;
                }
            }
            if(flag){
                SpuSaleAttr spuSaleAttr =  new SpuSaleAttr();
                spuSaleAttr.setBaseSaleAttrId(list.get(i).getBaseSaleAttrId());
                spuSaleAttr.setSaleAttrName(list.get(i).getSaleAttrName());
                spuSaleAttr.setSpuSaleAttrValueList(new ArrayList<>());
                spuSaleAttr.getSpuSaleAttrValueList().add(list.get(i));
                spuSaleAttrList.add(spuSaleAttr);
            }
        }
        return spuSaleAttrList;

        /*//两表单独查询
        List<SpuSaleAttrValue> list = spuSaleAttrValueMapper.selectList(new QueryWrapper<SpuSaleAttrValue>().eq("spu_id",spuId));
        if(list!=null){
            List<SpuSaleAttr> spuSaleAttrList = new ArrayList<>();
            QueryWrapper<SpuSaleAttr> wrapper = new QueryWrapper<>();
            wrapper.eq("base_sale_attr_id",list.get(0).getBaseSaleAttrId());
            wrapper.eq("spu_id",list.get(0).getSpuId());
            SpuSaleAttr spuSaleAttr = spuSaleAttrMapper.selectOne(wrapper);
            spuSaleAttr.setSpuSaleAttrValueList(new ArrayList<>());
            spuSaleAttrList.add(spuSaleAttr);
            for (int i = 0; i < list.size(); i++) {
                boolean flag = true;
                for (SpuSaleAttr saleAttr : spuSaleAttrList) {
                    if(list.get(i).getBaseSaleAttrId()==saleAttr.getBaseSaleAttrId()){
                        saleAttr.getSpuSaleAttrValueList().add(list.get(i));
                        flag = false;
                    }
                }
                if(flag){
                    QueryWrapper<SpuSaleAttr> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("base_sale_attr_id",list.get(i).getBaseSaleAttrId());
                    wrapper1.eq("spu_id",list.get(i).getSpuId());
                    SpuSaleAttr spuSaleAttr1 = spuSaleAttrMapper.selectOne(wrapper1);
                    spuSaleAttr1.setSpuSaleAttrValueList(new ArrayList<>());
                    spuSaleAttr1.getSpuSaleAttrValueList().add(list.get(i));
                    spuSaleAttrList.add(spuSaleAttr1);
                }
            }
            return spuSaleAttrList;
        }
        return null;*/
    }

    @Override
    public IPage<SkuInfo> list(Integer page, Integer limit) {
        return skuInfoMapper.selectPage(new Page<SkuInfo>(page, limit), null);
    }

    @Override
    public void onSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.ISSALE);
        skuInfoMapper.updateById(skuInfo);
        // TODO 发消息 给MQ 通知其他微服务 ES
    }

    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.CANCELSALE);
        skuInfoMapper.updateById(skuInfo);
        // TODO 发消息 给MQ 通知其他微服务 ES
    }

}
